<?php
class Login extends  Controller
{
    function Login()
    {
        parent::Controller();


       
		header("Content-Type: text/html; charset=utf-8");
		
    }
    
    function index()
    {
    	$username = "";
    	if(!empty($_COOKIE["admin_username"]))
    	{
    		$username = $_COOKIE["admin_username"];
    	}
    	$focus = $username!=""?"password":"username";
    	
    	$this->mytp->assign("focus",$focus);
    	$this->mytp->assign("username",$username);
	  	$this->mytp->tp("login");
    }
    function checklogin()
    {
    	
    	$this->load->library('input');
    	
        $username     = $this->input->post('username');
        $password     = $this->input->post('password');
        $state     = "1";
        if($username=="" || $password=="")
        {
        	e("用户名或密码不能为空");
        }
        
        if($state=="1")
        {
        	$query = $this->db->query("select * from ".table("users")." where username='".$username."' and state=1");
        	
			$userarr = $query->row_array();
			
		  
			
	        if($userarr)
	        {
	                if( md5($password) == $userarr['password'] )
	                {
	                  
	                    
	                    $data = array('lasttime'=>date("Y-m-d H:i:s"),'lastip'=>$_SERVER['REMOTE_ADDR']);
						$str = $this->db->update_string(table("users"),$data,'userid='.$userarr["userid"]);
						$this->db->query($str);
						
						$newdata = array
										(
						                   'username'  => $userarr["username"],
						                   'userid'     => $userarr["userid"],
						                   'lasttime' => $userarr['lasttime'],
						                   'lastip'  =>$userarr['lastip'],
						                   "companyid"=>$userarr['companyid'],
						                   "areaid"=>$userarr['areaid']
					               		);
	
						$this->session->set_userdata("systemdata",$newdata);
						
						if(empty($_COOKIE["admin_username"]))
						{
							 set_cookie("admin_username",$userarr["username"],86400*30);
						}else
						{
							if($userarr["username"]!=$_COOKIE["admin_username"])
							{
								set_cookie("admin_username",$userarr["username"],86400*30);
							}
						}
						
						
						header("Location: ".baseurl."main/index/");
						
						
					
	                } else
	                {
	                        e('密码错误');
	                }
	        } else
	        {
	                e('该用户不存在');
	        }
        }else
        {
        	 	$query = $this->db->query("select * from ".table("systemuser")." where username='".$username."'");
				$userarr = $query->row_array();
				
			
				
		        if($userarr)
		        {
		                if( md5($password) == $userarr['password'] )
		                {
		                  
		                    
		                    $data = array('lasttime'=>date("Y-m-d H:i:s"),'lastip'=>$_SERVER['REMOTE_ADDR']);
							$str = $this->db->update_string(table("systemuser"),$data,'uid='.$userarr["uid"]);
							$this->db->query($str);
							
							$newdata = array
											(
							                   'username'  => $userarr["username"],
							                   'userid'     => $userarr["uid"],
							                   'lasttime' => $userarr['lasttime'],
							                   'lastip'  =>$userarr['lastip']
						               		);
		
							$this->session->set_userdata("systemdata",$newdata);
		
							
							header("Location: ".baseurl."main/index/");
							
							
						
		                } else
		                {
		                        e('密码错误');
		                }
		        } else
		        {
		                e('该用户不存在');
		        }
        }
        
    }
    function logout($web="")
    {
    	$newdata = array
						(
						"systemdata"=>""
	               		);

        $this->session->unset_userdata($newdata);
        if(!empty($web))
        {
        	echo "<script>window.top.location.href='".baseurl."login/index/'</script>";
        }else
        {
        	header("Location: ".baseurl."login/index/");
        }
		
    }
    
    
}
?>